VERSION ?= v0.3 
CHART = deploy/helm/manager/$(VERSION)
RELEASE ?= gluenet
NAMESPACE ?= gluenet

AGENT_RELEASE ?= glue-agent
AGENT_CHART = deploy/helm/agent/$(VERSION)

ifeq ($(findstring glue,$(APP)), glue)
	REDEPLOY_VALUE = $(subst -,.,$(APP).enabled)
else
	REDEPLOY_VALUE = $(subst -,.,glue-$(APP).enabled)
endif

define refresh-manager

	echo "redeploy manager component: $(APP), use image -> $(IMAGE_NAME):$(IMAGE_TAG)"

	@helm -n $(NAMESPACE) upgrade $(RELEASE) $(CHART) \
	--reuse-values \
	--set $(REDEPLOY_VALUE)=false

	@helm -n $(NAMESPACE) upgrade $(RELEASE) $(CHART) \
	--reuse-values \
	--set $(REDEPLOY_VALUE)=true

endef

deploy:
	helm -n $(NAMESPACE) install --create-namespace $(RELEASE) $(CHART)

deploy-agent:
	helm -n $(NAMESPACE) install --create-namespace $(AGENT_RELEASE) $(AGENT_CHART)

redeploy-agent:
	@helm -n $(NAMESPACE) uninstall $(AGENT_RELEASE)
	@helm -n $(NAMESPACE) install --create-namespace $(AGENT_RELEASE) $(AGENT_CHART)

redeploy-app: image
	$(call refresh-manager)

refresh-app: build-env
	$(call refresh-manager)

refresh-frp:
	APP=frp make refresh-app